package com.gcit.seniorWish.web.modular.system.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.base.tips.Tip;
import com.gcit.seniorWish.core.exception.GunsException;

import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;

import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;


import com.gcit.seniorWish.web.common.constant.enums.SystemConfigurateEnum;
import com.gcit.seniorWish.web.common.persistence.model.SystemConfigurate;
import com.gcit.seniorWish.web.modular.system.service.ISystemConfigurateService;

import cn.hutool.core.util.StrUtil;

/**
 * 配置控制器
 *
 * @author fengshuonan
 * @Date 2018-03-27 13:31:27
 */
@Controller
@RequestMapping("/systemConfigurate")
public class SystemConfigurateController extends BaseController {

	private String PREFIX = "/system/systemConfigurate";

	@Autowired
	private ISystemConfigurateService systemConfigurateService;

	/**
	 * 跳转到配置首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "/systemConfigurate.html";
	}

	/**
	 * 跳转到编辑页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/edit")
	public String edit(SystemConfigurate configurate, Model model) {
		configurate = systemConfigurateService.selectById(configurate.getId());
		if (configurate == null)
			configurate = systemConfigurateService.selectOne(Condition.create().eq("configurate_key", SystemConfigurateEnum.SENIOR_WISH_END_DATE.getMessage()));
		model.addAttribute("configurate", configurate);
		return PREFIX + "/configurateEdit.html";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Tip save(@Valid SystemConfigurate configurate, BindingResult result) {
		if (result.hasErrors()) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		systemConfigurateService.mergeSystemConfig(configurate);
		return SUCCESS_TIP;
	}
}
